package com.tgy.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tgy.admin.Service.AdminArticleService;
import com.tgy.page.PageQuery;
import com.tgy.pojo.BlogArticle;
import com.tgy.result.GraceJSONResult;
import com.tgy.result.LayuiTableResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/21 21:01
 */
@Validated
@Api(value = "文章控制器", tags = {"文章管理"})
@RestController
@RequestMapping("/admin/article")
public class AdminArticleController {

    @Resource
    private AdminArticleService adminArticleService;

    @ApiOperation("获取文章列表")
    @SaCheckPermission
    @GetMapping("getArticleList")
    public LayuiTableResult getArticleList(BlogArticle bo, PageQuery pageQuery){
        return adminArticleService.getBlogArticleList(bo,pageQuery);
    }

    @ApiOperation("修改文章用户状态")
    @SaCheckPermission("blogarticle:update")
    @PostMapping("updateStatus")
    public GraceJSONResult updateStatus(BlogArticle bo){
        try {
            return adminArticleService.updateStatus(bo.getId(),bo.getStatus());
        }catch (Exception e){
            return GraceJSONResult.error();
        }
    }

    @ApiOperation("添加文章")
    @SaCheckPermission("blogarticle:add")
    @PostMapping("addBlogArticle")
    public GraceJSONResult addBlogArticle(BlogArticle blogArticle){
        return adminArticleService.addBlogArticle(blogArticle);
    }

    @ApiOperation("修改文章")
    @SaCheckPermission("blogarticle:update")
    @PostMapping("updateBlogArticle")
    public GraceJSONResult updateBlogArticle(BlogArticle blogArticle){
        return adminArticleService.updateBlogArticle(blogArticle);
    }

    @ApiOperation("删除文章")
    @SaCheckPermission("blogarticle:del")
    @PostMapping("delBlogArticle")
    // @PostMapping("delBlogArticle/{id}")
    public GraceJSONResult delBlogArticle(@ApiParam("ID必须传入") String id){
        return adminArticleService.delBlogArticle(id);
    }

    @ApiOperation("批量删除文章")
    @SaCheckPermission("blogarticle:del")
    @PostMapping("delBlogArticleIds")
    public GraceJSONResult delBlogArticleIds(@ApiParam("ids必须传入") String ids){
        List<String> list = new ArrayList<>(Arrays.asList(ids.split(",")));
        return adminArticleService.delBlogArticleIds(list);
    }

    @ApiOperation("根据ID查询文章")
    @SaCheckPermission
    @PostMapping("getBlogArticleById")
    public GraceJSONResult getBlogArticleById(@ApiParam("ID必须传入") String id){
        return adminArticleService.getBlogArticleById(id);
    }


}
